package developer.zyk.ability.helper;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import developer.zyk.ability.config.AIProperties;
import developer.zyk.global.common.consts.ChatCommonConstants;
import developer.zyk.global.user.bo.config.ModelConfig;
import developer.zyk.global.user.dao.entity.ChatUserDO;
import developer.zyk.global.user.dao.mappers.ChatUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;
import java.util.function.Supplier;

/**
 * @author zhangyongkang
 * @time 2025/5/15 20:57
 * @description
 */
@Component
public class ModelConfigHelper {

    private static AIProperties aiProperties;

    private static ChatUserMapper chatUserMapper;

    @Autowired
    public void setChatUserDO(ChatUserMapper chatUserMapper) {
        ModelConfigHelper.chatUserMapper = chatUserMapper;
    }

    @Autowired
    public void setAiProperties(AIProperties aiProperties) {
        ModelConfigHelper.aiProperties = aiProperties;
    }

    public static ModelConfig getModelConfigByUserId(Long userId) {
        ModelConfig config = JSONUtil.toBean(JSONUtil.parseObj(getConfigBy(userId)).getStr(ChatCommonConstants.ConfigConst.MODEL_CONFIG), ModelConfig.class);
        if (config == null) {
            return new ModelConfig();
        }
        return config;
    }

    private static String getConfigBy(Long userId) {
        ChatUserDO chatUserDO = chatUserMapper.selectById(userId);
        return chatUserDO.getSystemConfig();
    }

    public static String modelType(
            Supplier<String> supplier
    ) {
        String s = supplier.get();
        if (StrUtil.isBlank(s)) {
            s = aiProperties.getUserDefaultConfig().getModelType();
        }
        return s;
    }

    public static String systemPromptTemplate(
            Supplier<String> supplier
    ) {
        String template = supplier.get();
        if (StrUtil.isEmpty(template)) {
            return aiProperties.getUserDefaultConfig().getSystemPrompt();
        }
        return template;
    }

    public static Integer historySize(
            Supplier<Integer> supplier
    ) {
        Integer template = supplier.get();
        if (template == null) {
            template = aiProperties.getUserDefaultConfig().getHistorySize();
        }
        return Math.max(template, 1);
    }


    public static Boolean ragEnable(
            Supplier<Boolean> supplier
    ) {
        Boolean flag = supplier.get();
        if (Objects.isNull(flag)) {
            flag = aiProperties.getUserDefaultConfig().getRagEnable();
        }
        return Objects.equals(flag, Boolean.TRUE);
    }

    public static Integer ragTopN(
            Supplier<Integer> supplier
    ) {
        Integer topN = supplier.get();
        if (Objects.isNull(topN)) {
            topN = aiProperties.getUserDefaultConfig().getRagTopN();
        }
        return Math.max(topN, 1);
    }

    public static String ragTemplate(
            Supplier<String> supplier
    ) {
        String template = supplier.get();
        if (StrUtil.isEmpty(template)) {
            template = aiProperties.getUserDefaultConfig().getRagDefaultPrompt();
        }
        return template;
    }

    public static double similarityThreshold(
            Supplier<Double> supplier
    ) {
        Double value = supplier.get();
        if (value == null) {
            value = aiProperties.getUserDefaultConfig().getRagSimilarityThreshold();
        }
        return value;
    }

    public static boolean autoParse(
            Supplier<Boolean> supplier
    ) {
        Boolean b = supplier.get();
        if (Objects.isNull(b)) {
            b = aiProperties.getUserDefaultConfig().getAutoParseFile();
        }
        return b;
    }

    //内容随机
    //区间 0-2
    public static double frequencyPenalty(
            Supplier<Double> supplier
    ) {
        Double v = supplier.get();

        if (Objects.isNull(v)) {
            v = aiProperties.getUserDefaultConfig().getRagSimilarityThreshold();
        }
        return v;
    }

}
